package com.andy.app_core.net;


import com.andy.app_core.ConfigKeys;
import com.andy.app_core.ProjectInit;
import com.andy.app_core.net.rx.RxRestService;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @version V1.0
 * @描述:
 * @Company:
 * @auth: Created by AN .
 * @time: 2020/3/26 14:01
 * @des
 */
public final class RestCreator {


    /**
     * 产生一个全局的Retrofig客户端
     */
    private static final class RetrofitHolder{
        private static final String BASE_URL= ProjectInit.getConfiguration(ConfigKeys.API_HOST);
        private static final Retrofit RETROFIT_CLIENT=
                new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .client(OKHttpHolder.OK_HTTP_CLIENT)
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//Rxjava适配器
                        .build();
    }
    private static final class OKHttpHolder{
        private static final int TIME_OUT=60;
        private static final OkHttpClient OK_HTTP_CLIENT=
                new OkHttpClient.Builder()
                        .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                        .build();
    }

    //提供接口让调用者得到retrofit对象
    public static final class RestServiceHolder{
        public static final RestService REST_SERVICE= RetrofitHolder.RETROFIT_CLIENT.create(RestService.class);
    }

    //获取对象
    public static RestService getRestService(){
        return RestServiceHolder.REST_SERVICE;
    }

    public static final class RxRestServiceHolder{
        public static final RxRestService REST_SERVICE= RetrofitHolder.RETROFIT_CLIENT.create(RxRestService.class);
    }

    public static RxRestService getRxRestService() {
        return RxRestServiceHolder.REST_SERVICE;
    }
}
